package com.example.demo1;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * 字处理；
 * 作用：1.统计出 文章中字，词的次数；
 * https://stackoverflow.com/questions/26282009/how-to-count-the-number-of-occurrences-of-each-word
 */
public class WordDetail {
    public static void main(String[] args) throws IOException {
        Map<String, Integer> countByWords = new HashMap<String, Integer>();
        Scanner s = new Scanner(new File("F:\\lys\\software\\eclipse\\artifacts.xml"));
        while (s.hasNext()) {
            String next = s.next();
            Integer count = countByWords.get(next);
            if (count != null) {
                countByWords.put(next, count + 1);
            } else {
                countByWords.put(next, 1);
            }
        }
        s.close();

        //map输出
        for(Map.Entry<String, Integer> entry : countByWords.entrySet() ){
            System.out.println("Key =' "+entry.getKey()+"  '  ,   value ='"+entry.getValue()+"'");
        }
    }
}
